//
//  Dock.swift
//  foreign_swift
//
//  Created by hdr on 15-4-19.
//  Copyright (c) 2015年 hdr. All rights reserved.
//

import UIKit

let dock_height = 44

let titles=["检测","服务","系统","我"]
let images=["tabbar_detection","tabbar_service","tabbar_system","tabbar_my"]

protocol DockClickDelegate {
    func onDockClick(dockIndex:Int);
}



class Dock :UIView {
    var dockDatas:[DockData]
    var dockClickDelegate:DockClickDelegate?
    
    override init(frame: CGRect) {
        dockDatas = Array<DockData>()
        super.init(frame: frame)
        for i in 0..<4 {
            self.addItem(images[i], title: titles[i])
        }
//        self.backgroundColor = UIColor.grayColor()
        self.layer.contents = UIImage(named: "tabbar_background.png")?.CGImage

    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func addItem(img:String,title:String){
        dockDatas.append(DockData(title: title, imageFilename: img))
    }
    
    func adjustView(){
        let width = self.bounds.size.width
        let itemWidth = Int(width/4);
        for i in 0..<dockDatas.count {
            let v = UIButton()
            v.frame = CGRect(origin: CGPoint(x: i * itemWidth, y: 2), size: CGSize(width: itemWidth, height: dock_height))

            let imgView = ViewHelper.initImageViewWithImageName(dockDatas[i].imageFilename)
            imgView.center = CGPoint(x: itemWidth/2, y: 12)
            let label = UILabel()
            label.frame = CGRect(x: 0, y: 23, width: itemWidth, height: 20)
            label.textAlignment = NSTextAlignment.Center
            label.text = dockDatas[i].title
            label.font = UIFont.systemFontOfSize(12)
            label.textColor = UIColor.themeColor
            
            v.addSubview(imgView)
            v.addSubview(label)
            
            v.tag = i
            
            v.addTarget(self, action: "onDockItemClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
            dockDatas[i].btn = v
            dockDatas[i].titleLabel = label
            dockDatas[i].imageView = imgView
            
            self.addSubview(v)
        }
        setSelectBtnView(0)

    }
    
    func onDockItemClick(sender:UIButton){

        setSelectBtnView(sender.tag)
        
        
    }
    
    func setSelectBtnView(index:Int){
        
        for i in 0..<dockDatas.count {
            if i == index {
                let btn = dockDatas[index].btn
                let label = dockDatas[index].titleLabel
                let imageView = dockDatas[index].imageView
                
                let selectFileName = dockDatas[index].imageFilename+"_selected"
                imageView.image = UIImage(named: selectFileName)
                btn.backgroundColor = UIColor.themeColor
                label.textColor = UIColor.whiteColor()
            }else{
                let btn = dockDatas[i].btn
                let label = dockDatas[i].titleLabel
                let imageView = dockDatas[i].imageView
                
                imageView.image = UIImage(named: dockDatas[i].imageFilename)
                btn.backgroundColor = nil
                label.textColor = UIColor.themeColor
            }
        }
        dockClickDelegate?.onDockClick(index)
    }
    
}



struct DockData {
    let title:String
    let imageFilename:String
    var btn:UIButton!
    var titleLabel:UILabel!
    var imageView:UIImageView!
    
    init(title:String,imageFilename:String){
        self.title = title
        self.imageFilename = imageFilename
    }
}
